from typing import List, Union

from emi.bar.data import Interval


def make_k_bars_data(bars:List,is_daily_time = False):
    time_list = []  ## 时间列表
    time_value = []

    candle_values = []
    volume_list = []
    amount_list = []

    data_map = {}
    min_value = None
    max_value = None
    for bar in bars:
        # if is_daily_time:
        #     time_list.append(f"{time.strftime('%Y/%m/%d')}")
        # else:
        try:
            time = bar.time
            if is_daily_time:
                time_list.append(f"{time.date()}")
            else:
                time_list.append(f"{time.strftime('%Y/%m/%d %H:%M')}")
            time_value.append(time)
        except:
            try:

                time = bar.datetime
                if is_daily_time:
                    time_list.append(f"{time.date()}")
                else:
                    time_list.append(f"{time.strftime('%Y/%m/%d %H:%M')}")
                time_value.append(time)
            except:
                time_list.append(f"{bar.hh_mm}")
        if max_value is None or max_value < bar.high:
            max_value = bar.high
        if min_value is None or min_value > bar.low:
            min_value = bar.low
        candle_values.append([bar.open, bar.close, bar.low, bar.high,bar.pre_close])
        volume_list.append(bar.volume)
        amount_list.append(bar.amount)

    data_map['k_bar'] = candle_values
    data_map['volume'] = volume_list
    data_map['amount'] = amount_list
    data_map['time'] = time_list
    data_map['time_value'] = time_value
    data_map['max_value'] = max_value
    data_map['min_value'] = min_value
    return data_map


def calculate_ma(day_count: int, values:[]):
    result: List[Union[float, str]] = []
    for i in range(len(values)):
        if i < day_count:
            result.append("-")
            continue
        sum_total = 0.0
        for j in range(day_count):
            sum_total += float(values[i - j][1])
        result.append(abs(float("%.3f" % (sum_total / day_count))))
    return result